<template>
  <el-dialog
    :title="!dataForm.articleId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="2vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px">
      <el-form-item label="所属技能" prop="skillsId">
        <el-select v-model="dataForm.skillsId" placeholder="请选择">
          <el-option
            v-for="item in perskiList"
            :key="item.skillsId"
            :label="item.skillsName"
            :value="item.skillsId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input v-model="dataForm.articleTitle" placeholder="文章标题" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleContent">
        <quill-editor
          ref="myQuillEditor"
          v-model="dataForm.articleContent"
          :options="editorOption"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @change="onEditorChange($event)"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqperskiApi from '@/api/personalskills'
import reqApi from '@/api/TechnicalArticle'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        articleId: '',
        skillsId: '',
        articleTitle: '',
        articleContent: '',
        creationTime: '',
        modificationTime: ''
      },
      perskiList: [],
      dataRule: {
        skillsId: [
          { required: true, message: '所属技能不能为空', trigger: 'blur' }
        ],
        articleTitle: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleContent: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ],
        creationTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        modificationTime: [
          { required: true, message: '修改时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.articleId = id || ''
      this.visible = true
      reqperskiApi.getList().then(({ data }) => {
        this.perskiList = data.page.list
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.articleId) {
          reqApi.getInfo(this.dataForm.articleId).then(({ data }) => {
            if (data) {
              this.dataForm.skillsId = data.technicalArticle.skillsId
              this.dataForm.articleTitle = data.technicalArticle.articleTitle
              this.dataForm.articleContent = data.technicalArticle.articleContent
              this.dataForm.creationTime = data.technicalArticle.creationTime
              this.dataForm.modificationTime = data.technicalArticle.modificationTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.articleId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
    onEditorReady(editor) { // 准备编辑器

    },
    onEditorBlur() {}, // 失去焦点事件
    onEditorFocus() {}, // 获得焦点事件
    onEditorChange() {} // 内容改变事件
  }
}
</script>

<style>
.ql-editor{
    min-height: 350px;
    max-height: 350px;
}
</style>
